<?php

namespace Admin\Controller;
class TemplateController extends CommonController{
    private $TplPath;
    public function _initialize() {
        parent::_initialize();
        $this->TplPath = APP_PATH . "Home/Templets/";
    }
    
    public function index() {
        $tpls = scan_dir($this->TplPath, 1);
        
        $tpl_configs = array();
        $conf = fast_cache('web_config', '', YDCMS_DATA);
        foreach ($tpls as $file) {
            if(is_dir($this->TplPath . $file)){
                $config_file = $this->TplPath . $file . "/config.ini";
                if(file_exists($config_file)){
                    $config = load_config($config_file);
                    $config['litpic'] = isLitpic($this->TplPath . $file . "/" . $config['litpic']);
                    $config['isdefault'] = $file == $conf['DEFAULT_THEME'] ? 1 : 0;
                    $config['tplname'] = $file;
                    $tpl_configs[$file] = $config;
                }
            }
        }
        $this->assign("list", $tpl_configs);
        //var_dump($tpl_configs);die;//DEFAULT_THEME
        $this->display();
    }
    
    /**
     * 设置默认模板
     */
    public function setDefault() {
        $theme = I("theme");
        if(file_exists($this->TplPath . $theme) && is_dir($this->TplPath . $theme)){
            $web_conf = fast_cache("web_config", '', YDCMS_DATA);
            $config['DEFAULT_THEME'] = $theme;
            $new_config = array_merge($web_conf, $config);
            if(false === (fast_cache("web_config", $new_config, YDCMS_DATA))){
                return $this->errorReturn('设置默认模板失败！');
            }

            return $this->successReturn('设置默认模板成功！');
        }
    }
}
